package com.sunye.rabbitBattle;


import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/battleController")
public class RabbitMqBattleController  implements InitializingBean {
    @Autowired
    private RabbitTemplate rabbitTemplate;
//    @Autowired
//    private RedisTemplate redisTemplate;

    private static Map<Integer,Boolean> seckillNumber = new HashMap<>();

    //解耦（发送短信和邮箱  如果发送邮箱不用了可以注释掉代码  把2个逻辑分开了）
    @RequestMapping("/asynchronous")
    public String asynchronous(){
        Map map =  new HashMap();
        map.put("phone","13756491443");
        map.put("email","1559464975@qq.com");
        rabbitTemplate.convertAndSend("asynchronous.change","",map);
        return  "请求成功！";
    }


    //异步 下单成功了异步去处理其他不重要的信息比如给用户发送下单成功的短信
    @RequestMapping("/decoupling")
    public String decoupling(){
        Map map = new HashMap();
        map.put("goodId",1);
        map.put("userId",14826);
        // TODO 此处应该写入数据库下单操作
        rabbitTemplate.convertAndSend("decoupling.change","",map);
        return "下单成功";
    }

    //削峰--秒杀
//    @RequestMapping("/spikeOrder")
//    public String spikeOrder(Integer goodsId,Integer userId){
//        if(goodsId ==null || userId== null){
//            return "goodsId或userId不能为空";
//        }
//        ValueOperations valueOperations = redisTemplate.opsForValue();
//        //内存标记  查看是否有库存
//        if (seckillNumber.get(goodsId)) {
//            return "error,库存不足";
//        }
//        //减少redis库存数量
//        Long decrement = valueOperations.decrement("stock:"+goodsId);//按照库存字段递减
//        if(decrement<0){
//            seckillNumber.put(goodsId,false);
//            //此时decrement为-1也就是库存为-1  应该把库存加1为0
//            valueOperations.decrement("stock:"+goodsId);
//            return "error,库存不足";
//        }
//        //rabbit异步操作
//        Map map = new HashMap();
//        map.put("userId",userId);
//        map.put("goodsId",goodsId);
//        this.sendSeckillMessage(map);
//        return "下单排队中！";
//    }

    //客户端轮训查看是否秒杀成功--1分钟计算一次
    @RequestMapping("/viewOrder")
    public String viewOrder(Integer goodsId,Integer userId){
        //TODO  查看数据库是否秒杀成功
        return "下单成功！";
    }


    //发送秒杀信息
    public void sendSeckillMessage(Map map){
        System.out.println("发送消息======");
        rabbitTemplate.convertAndSend("seckillExchange","seckill.message",map);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
//        //初始化redis库存数量
//        redisTemplate.opsForValue().set("stock:"+1,10);
//        redisTemplate.opsForValue().set("stock:"+2,10);
//        redisTemplate.opsForValue().set("stock:"+3,10);
        //商品是否有库存
        seckillNumber.put(1,false);
        seckillNumber.put(2,false);
        seckillNumber.put(3,false);
    }
}
